<template>
  <div>
    <search :auto-fixed="false" @on-submit="handleChange" @on-change="handleChange" v-model="condition.text"></search>
    <load-more v-show="!cars || !cars.length" :show-loading="false" tip="暂无数据" background-color="#fbf9fe"></load-more>
    <cell @click.native.prevent="handleItemClick(item)" v-for="(item, index) in cars" :key="index" :title="item.hphm"
          :value="`${item.jylsh}-${item.jycs}`" is-link></cell>
  </div>
</template>

<script>
  export default {
    name: "List",
    data: function () {
      return {
        cars: [],
        condition: {
          text: '',
          jyxm: ''
        },
        page: {
          size: 20,
          current: 1
        },
        timeTag: null
      };
    },
    methods: {
      handleChange() {
        if (this.timeTag) {
          clearTimeout(this.timeTag);
        }
        this.timeTag = setTimeout(() => {
          this.handleSearch();
        }, 1000);
      },
      handleSearch() {
        const requestBody = {};
        const jyxm = this.condition.jyxm;
        const text = this.condition.text;
        requestBody.api = this.$api.connDb;

        requestBody.data = {};
        requestBody.data.sql = `select top ${this.page.size * this.page.current} jylsh,jycs,hphm,kssj,sjsj,jyjgbh,hpzl,clsbdh FROM tab_TestItemState WHERE jyxm = '${jyxm}' and (jssj is NULL OR jssj = '')`;
        if (text) {
          requestBody.data.sql += ` and (hphm like '%${text}%' OR jylsh like '%${text}%') `;
        }
        requestBody.data.sql += ' ORDER BY jylsh desc'
        this.$http.post(this.$store.getters.uri, requestBody).then(res => {
          const {data} = res;
          const {code} = data;
          if (!(code === 200 || code === '200')) {
            this.$error(data.message);
            return;
          }
          this.cars = data.data;
          this.$success(data.message, null);
/*          for (let i = 0; i < 5; i++) {

            let parse = JSON.parse(JSON.stringify(data.data));
            parse.forEach(item => {
              this.cars.push(item);
            })
          }*/
        }).catch(e => {
          this.$error(e);
          console.log(e);
        });
      },
      handleItemClick(car) {
        this.$store.commit('setCarInfo', car);
        if (this.$store.state.carInfo.jyxm === 'Z1') {
          this.$store.commit("setTitle", "整备质量")
          this.$router.push('/car/Curb');
          return;
        }
        this.$store.commit("setTitle", "外廓")
        this.$router.push('/car/CarSize');
      }
    },
    created() {
      this.condition.jyxm = this.$store.state.carInfo.jyxm;
      this.handleSearch();
      this.$store.commit('setCarInfo', {});
    }
  }
</script>

<style scoped>

</style>
